package com.stony.poie.demo;

import java.io.FileInputStream;
import java.io.IOException;

import org.apache.poi.hpsf.PropertySetFactory;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.poifs.eventfilesystem.POIFSReader;
import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;
import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;

public class GetTitle {

	public static void main(String[] args) throws IOException {

		String filename = "D:\\demo.xls";

		POIFSReader reader = new POIFSReader();

		reader.registerListener(new MyPOIFSReaderListener(),

		"\005SummaryInformation");

		reader.read(new FileInputStream(filename));

	}

	static class MyPOIFSReaderListener implements POIFSReaderListener {

		public void processPOIFSReaderEvent(POIFSReaderEvent event) {

			SummaryInformation info = null;

			try {

				info = (SummaryInformation) PropertySetFactory.create(event

				.getStream());

			} catch (Exception ex) {

				throw new RuntimeException("Property set stream \""

				+ event.getPath() + event.getName() + "\": " + ex);

			}

			String title = info.getTitle();

			/* ?????????????????????????????????????????????? */

			if (title == null || title.trim().equals(""))

				System.out.println("Document has no title.");

			else

				System.out.println("Title: \"" + title + "\"");

		}

	}

}
